using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_AddressEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        Address address;

        if (Int32.TryParse(Request["id"], out id))
            address = Address.Load(id);
        else
            address = null;

        if (address != null)
        {
            if (!string.IsNullOrEmpty(address.AddressTitle))
				tbxAddressTitle.Text = address.AddressTitle;
			if (!string.IsNullOrEmpty(address.AddressLines))
				tbxAddressLines.Text = address.AddressLines;
			if (!string.IsNullOrEmpty(address.City))
				tbxCity.Text = address.City;
			if (!string.IsNullOrEmpty(address.State))
				tbxState.Text = address.State;
			if (!string.IsNullOrEmpty(address.Country))
				tbxCountry.Text = address.Country;
			if (!string.IsNullOrEmpty(address.Phone))
				tbxPhone.Text = address.Phone;
			if (!string.IsNullOrEmpty(address.ZipCode))
				tbxZipCode.Text = address.ZipCode;
			if (!string.IsNullOrEmpty(address.AddressHolderFirstName))
				tbxAddressHolderFirstName.Text = address.AddressHolderFirstName;
			if (!string.IsNullOrEmpty(address.AddressHolderLastName))
				tbxAddressHolderLastName.Text = address.AddressHolderLastName;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string addressTitle = Security.BindStringInput(tbxAddressTitle.Text, _securityLevel);
		string addressLines = Security.BindStringInput(tbxAddressLines.Text, _securityLevel);
		string city = Security.BindStringInput(tbxCity.Text, _securityLevel);
		string state = Security.BindStringInput(tbxState.Text, _securityLevel);
		string country = Security.BindStringInput(tbxCountry.Text, _securityLevel);
		string phone = Security.BindStringInput(tbxPhone.Text, _securityLevel);
		string zipCode = Security.BindStringInput(tbxZipCode.Text, _securityLevel);
		string addressHolderFirstName = Security.BindStringInput(tbxAddressHolderFirstName.Text, _securityLevel);
		string addressHolderLastName = Security.BindStringInput(tbxAddressHolderLastName.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (Address.Save(
				id, 
				addressTitle,
				addressLines,
				city,
				state,
				country,
				phone,
				zipCode,
				addressHolderFirstName,
				addressHolderLastName
                ))
                    Response.Redirect("addresslist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            Address address = Address.Create(
				addressTitle,
				addressLines,
				city,
				state,
				country,
				phone,
				zipCode,
				addressHolderFirstName,
				addressHolderLastName
                );

            if (address != null)
                Response.Redirect("addresslist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("addresslist.aspx");
    }
}
